package com.sondertara.joya.pool.datasource;


import com.sondertara.common.invocation.proxy.ProxyUtils;
import com.sondertara.common.invocation.proxy.core.MethodInterceptor;
import com.sondertara.common.invocation.proxy.core.MethodMatcher;
import com.sondertara.joya.pool.connection.PoolEntity;

import java.sql.Connection;

/**
 * 连接代理
 *
 * @author: ZhangHe
 * @since: 2020/10/28 9:26
 */
public abstract class ConnectionProxyDecorate {

    public static Connection proxyConnection(PoolEntity<Connection> poolEntity) {
        //close 改为回收
        MethodInterceptor interceptor = targetMethod -> {
            poolEntity.recycle(System.currentTimeMillis());
            return null;
        };

        return ProxyUtils.proxy(poolEntity.getConnection(), interceptor.when(MethodMatcher.withName("close")));


    }
}
